package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Cities;
import com.woniuxy.entity.DTO.WarehousesDTO;
import com.woniuxy.entity.Districts;
import com.woniuxy.service.CitiesService;
import com.woniuxy.service.DistrictsService;
import com.woniuxy.service.WarehousesService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    private WarehousesService warehousesService;

    @Resource
    private CitiesService citiesService;

    @Resource
    private DistrictsService districtsService;

    @GetMapping("/queryList")
    public ResponseData queryList() {
        List<WarehousesDTO> warehousesDTOS = warehousesService.queryWarehousesInfo();
        return ResponseData.success(warehousesDTOS);
    }

    @GetMapping("/queryCities")
    public ResponseData queryCities(){
        List<Cities> cities = citiesService.queryCities();
        return ResponseData.success(cities);
    }

    @GetMapping("/queryDistricts/{CityId}")
    public ResponseData queryDistricts(@PathVariable Integer CityId){
        List<Districts> districts = districtsService.queryDistricts(CityId);
        return ResponseData.success(districts);
    }

    @PostMapping("/queryListByCondition")
    public ResponseData queryListByCondition(@RequestBody PageParam<WarehousesDTO> pageParam){
        PageInfo pageInfo = warehousesService.queryWarehouseByCondition(pageParam);
        return ResponseData.success(pageInfo);
    }


    @GetMapping("/changeStatus/{id}")
    public ResponseData changeStatus(@PathVariable Integer id){
        /**
         * 判断逻辑，仓库下若存在为解绑人员，则无法停用仓库
         */
        boolean existRiderOrCourier = warehousesService.isExistRiderOrCourier(id);
        if(existRiderOrCourier){
            return ResponseData.fail(ResponseEnum.UNABLE_TO_DEACTIVATE_WAREHOUSE);
        }else{
            Integer integer = warehousesService.changeStatus(id);
            return ResponseData.success(integer);
        }
    }

    @PostMapping("/addWarehouse")
    public ResponseData addWarehouse(@RequestBody WarehousesDTO warehousesDTO){
        warehousesService.addWarehouse(warehousesDTO);
        return ResponseData.success();
    }

    @GetMapping("/getWarehouseDetail/{warehouseId}")
    public ResponseData getWarehouseDetail(@PathVariable Integer warehouseId){
        WarehousesDTO warehouseDetail = warehousesService.getWarehouseDetail(warehouseId);
        return ResponseData.success(warehouseDetail);
    }

    @PostMapping("/updateWarehouse")
    public ResponseData updateWarehouse(@RequestBody WarehousesDTO warehousesDTO){
        System.out.println("111");
        warehousesService.updateWarehouse(warehousesDTO);
        return ResponseData.success();
    }

}
